﻿// 大气监测 图形绘制统一方法
(function () {
    var instance;
    window.AnalyseHelp = function (chartSelector, dataSource, pollutantNameObj, pollutantUnitObj, pollutantConst, chartTitle) {
        if (instance == undefined) {
            instance = this;
            this.ChartSelector = chartSelector;
            this.DataSource = dataSource;
            this.PollutantNameObj = pollutantNameObj;
            this.PollutantUnitObj = pollutantUnitObj;
            this.PollutantConst = pollutantConst;
            this.Title = chartTitle;
            this.DrawChart = function (contentType, ForecastScheme) {
                // 子标题，纵轴标识
                var subTitle = "";
                if (this.PollutantNameObj.hasOwnProperty(contentType)) {
                    subTitle += this.PollutantNameObj[contentType];
                }
                if (this.PollutantUnitObj.hasOwnProperty(contentType)) {
                    subTitle += "(" + this.PollutantUnitObj[contentType] + ")";
                }
                //横轴数组
                var categoriesArray = [];
                //数据过滤
                //第一次过滤
                var ceshiObject = [];
                for (var i = 0, length = this.DataSource.length; i < length; i++) {
                    var dataItem = this.DataSource[i];
                    if (dataItem.ForecastScheme == "-024小时" || dataItem.ForecastScheme == ForecastScheme) {
                        ceshiObject.push(dataItem);
                    }
                    //if (dataItem.ForecastScheme == ForecastScheme) {
                    //    ceshiObject.push(dataItem);
                    //}
                }
                var seriesObject = {};

                for (var i = 0, length = ceshiObject.length; i < length; i++) {
                    var dataItem = ceshiObject[i];
                    if (categoriesArray.indexOf(dataItem["DataTime"]) === -1) {
                        if (!categoriesArray.hasOwnProperty(dataItem["DataTime"])) {
                            categoriesArray.push(dataItem["DataTime"]);
                        }
                    }
                    if (!seriesObject.hasOwnProperty(dataItem["ForecastModelName"])) {
                        seriesObject[dataItem["ForecastModelName"]] = [];
                    }
                    if (dataItem.hasOwnProperty(contentType)) {
                        seriesObject[dataItem["ForecastModelName"]].push(dataItem[contentType]);
                    } else {
                        seriesObject[dataItem["ForecastModelName"]].push(null);
                    }
                }
                var seriesArray = [];
                for (var key in seriesObject) {
                    if (seriesObject.hasOwnProperty(key)) {
                        seriesArray.push({
                            type: "spline",
                            name: key,
                            data: seriesObject[key]
                        });
                    }
                }
                // 限值组
                var yPlots = [];
                if (this.PollutantConst.PollutantLevelValue.hasOwnProperty(contentType)) {
                    yPlots = this.PollutantConst.PollutantLevelValue[contentType];
                }
                // x轴间距
                var step = parseInt(categoriesArray.length / 6);
                if (0 === step) {
                    step = 1;
                }


                this.ChartSelector.highcharts({
                    lang: {
                        printChart: '打印图表',
                        downloadPNG: '下载JPEG 图片',
                        downloadJPEG: '下载JPEG文档',
                        downloadPDF: '下载PDF 文件',
                        downloadSVG: '下载SVG 矢量图',
                        contextButtonTitle: '下载图片',
                        noData: '无数据'
                    },
                    title: {
                        text: this.Title
                    },
                    subtitle: {
                        text: subTitle
                    },
                    colors: this.PollutantConst.ColorArray,
                    yAxis: {
                        title: {
                            text: subTitle
                        },
                        plotLines: yPlots
                    },
                    xAxis: {
                        categories: categoriesArray,
                        labels: {
                            step: step
                        }
                    },
                    credits: {
                        enabled: false
                    },
                    series: seriesArray,
                    noData: {
                        style: {
                            fontWeight: 'bold',
                            fontSize: '15px',
                            color: '#303030'
                        }
                    }
                });
            }
        }
        return instance;
    }
})();